import Router from '../../utils/router';
import base from '../../config/base.js';
import service from '../../services/groupdragon';
import Record from '../../utils/page-record.js';
import Record_server from '../../services/record-stat';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    icon_play: base.static + 'store/play2.png',
    icon_group: base.static + 'groupdragon/groupdDragon1.png',
    icon_mygroup: base.static + 'groupdragon/myFree2.png',
    icon_next: base.static + 'collage/next.png',
    icon_mask_sellout: base.static + 'collage/out.png',
    icon_noorder: base.static + '/collage/no-order-list.png',
    myGroupList: [], //我的群接龙
    endMyBloon: true,
    loadMyBloon: true,
    pageMyGroup: 0,
    nomoreMy: true,
    swiperList: '', //轮播
    navcur: 1, //进行中 已完成
    noMylist: true, //没有订单
    share:false,//组件canvas
    datas:{},//传给canvas组件
    segsid:'',//点击接龙色segsid
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    if (options.method && options.method == 'navigateBack') {
    } else {
      that.getMyGroupList(1,1)
    }
  },
  //我的群接龙列表
  getMyGroupList(page, types) {
    let that = this;
    let params = {
      page: page,
      status: types,
      offset: 20
    }
    service.solitaireorderlist(params, function (res) {
      if (res && res.code == 1) {
        let mylist = that.data.myGroupList.concat(res.data.data);
        that.setData({
          myGroupList: mylist,
          pageMyGroup: page + 1,
          endMyBloon: true,
        })
        if (mylist.length > 0 && mylist.length >= res.data.count) {
          that.setData({
            nomoreMy: false, //没有更多了
            loadMyBloon: false,
          })
        }
      } else if (res && res.code == 413) {//没有订单
        that.setData({
          noMylist: false
        })
      }
      wx.stopPullDownRefresh()
    })
  },
  // 点击底部选项卡
  chooseTab: function (e) {
    let that = this;
    Router.go('groupdragon', ``,'redirectTo')
  },
  // 点击进行中 已完成
  groupNav: function (e) {
    let that = this;
    let navCurrent = e.currentTarget.dataset.navcur;
    if (navCurrent == that.data.navcur) {
    } else {
      that.setData({
        navcur: navCurrent,
        myGroupList: [], //我的群接龙
        endMyBloon: true,
        loadMyBloon: true,
        pageMyGroup: 0,
        nomoreMy: true,
        noMylist: true,
      })
      that.getMyGroupList(1, navCurrent)
    }
  },
  //去订单详情
  goorders(e) {
    let orid = e.currentTarget.dataset.orid;
    Router.go('orderDetail', `orid=${orid}`);
  },
  // 记录分享按钮
  btnClick: function (e) {
    console.log('.....点击了')
    let gard = e.currentTarget.dataset.gard;
    for(var i=0;i<gard.length;i++){
      gard[i].goods_grads_price=gard[i].price
    }
    let datas={
      goodsPic:[{
        url: e.currentTarget.dataset.goodspic
      }],
      goods:{
        name: e.currentTarget.dataset.name
      },
      grad:gard
    }
    this.setData({
      share:true,
      datas:datas,
      bslnid:e.currentTarget.dataset.bslnid,
      segsid:e.currentTarget.dataset.segsid,
      imgurl:e.currentTarget.dataset.url
    })
    let p = getCurrentPages().pop();
    Record.clickButton(p, 'Invitation_groupDragon')
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    let that = this;

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
  onPullDownRefresh: function () {
    let that = this;
      that.setData({
        myGroupList: [], //我的群接龙
        endMyBloon: true,
        loadMyBloon: true,
        pageMyGroup: 0,
        nomoreMy: true,
        noMylist: true,
      });
      that.getMyGroupList(1, that.data.navcur)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    if (that.data.loadMyBloon && that.data.endMyBloon) {
      that.setData({
        endMyBloon: false
      })
      that.getMyGroupList(that.data.pageMyGroup, that.data.navcur);
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    let that = this;
    let segsid =this.data.segsid;
    let bslnid =this.data.bslnid;
    let user_id = wx.getStorageSync('user_info').id;
    let paths = '/pages/detail/dragon?segsid=' + segsid + '&sharerecord=groupDragoncard' + '&store_id=' + bslnid + '&user_id=' + user_id;
    let imgurl = 'https:' + this.data.imgurl
    return {
      title: '力度太大，我好害怕！多人购买，居然还会退差价！',
      path: paths,
      imageUrl: imgurl
    }
  }
})